package converter;


	import java.io.FileOutputStream;
	import java.text.DateFormat;
	import java.text.SimpleDateFormat;
	import java.util.Date;
	import java.util.List;
	import java.awt.Desktop;

	import java.io.File;
	import model.Product;
	import persistency.PersistencyException;
	import persistency.Product_CRUD;

	import com.itextpdf.text.BaseColor;
	import com.itextpdf.text.Document;
	import com.itextpdf.text.DocumentException;
	import com.itextpdf.text.Element;
	import com.itextpdf.text.Font;
	import com.itextpdf.text.Paragraph;
	import com.itextpdf.text.Phrase;
	import com.itextpdf.text.pdf.PdfPCell;
	import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;


	public class MakePDF {
//		private static String File = "c:/temp/ProductLijst-";
		
		private static Font catFont = new Font(Font.FontFamily.HELVETICA, 18,
				Font.BOLD);
		private static Font redFont = new Font(Font.FontFamily.HELVETICA, 12,
				Font.NORMAL, BaseColor.RED);
		private static Font subFont = new Font(Font.FontFamily.HELVETICA, 16,
				Font.BOLD);
		private static Font smallBold = new Font(Font.FontFamily.HELVETICA, 12,
				Font.BOLD);

		public static String make() {
			try {
				String file = "c:/temp/ProductLijst-";
				Document document = new Document();
				DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				file += df.format(new Date()) + ".pdf";
				PdfWriter.getInstance(document, new FileOutputStream(file));
				document.open();
				addMetaData(document);
				//addTitlePage(document);
				addContent(document);
				document.close();
				showPDF(file);
				return "success";
			} catch (Exception e) {
				e.printStackTrace();
				return "error";
			}
		}

		// iText allows to add metadata to the PDF which can be viewed in your Adobe
		// Reader
		// under File -> Properties
		private static void addMetaData(Document document) {
			document.addTitle("Product overzicht");
			document.addSubject("");
			document.addKeywords("");
			document.addAuthor("Johan Willems");
			document.addCreator("Johan Willems");
		}

        private static void showPDF(String file) {
        try {
        	File pdfFile = new File(file);
            if (pdfFile.exists()) {
            	if (Desktop.isDesktopSupported()) {
            		Desktop.getDesktop().open(pdfFile);
                } else {
                	System.out.println("Awt Desktop is not supported!");
                }
            } else {
            	System.out.println("File is not exists!");
            }
        } catch (Exception ex) {
        	ex.printStackTrace();
        }
        }

		private static void addContent(Document document) throws DocumentException {
			Paragraph preface = new Paragraph();
			// We add one empty line
			addEmptyLine(preface, 1);
			// Lets write a big header
			preface.add(new Paragraph("Producten lijst", catFont));
			preface.add(new Paragraph("Report generated by: " + System.getProperty("user.name") + ", " + new Date(),smallBold));
			addEmptyLine(preface, 3);

			document.add(preface);
			PdfPTable table = new PdfPTable(4);

			PdfPCell c1 = new PdfPCell(new Phrase("ID"));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("Omschrijving"));
			c1.setHorizontalAlignment(Element.ALIGN_LEFT);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("Eenheid"));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);

			c1 = new PdfPCell(new Phrase("Actief (0/1)"));
			c1.setHorizontalAlignment(Element.ALIGN_CENTER);
			table.addCell(c1);
			table.setHeaderRows(1);

			Product_CRUD crud = new Product_CRUD();
			
			try {
				List<Product> producten = crud.getAll();
				for(Product pr : producten) {
					//System.out.println(pr.toString());
					table.addCell(Long.toString(pr.getProductId()));
					table.addCell(pr.getDescription());
					table.addCell(pr.getProductType());
					table.addCell(pr.getActiveText());
				}					
			} catch (PersistencyException e) {
				e.printStackTrace();
			}
			document.add(table);

		}

		private static void addEmptyLine(Paragraph paragraph, int number) {
			for (int i = 0; i < number; i++) {
				paragraph.add(new Paragraph(" "));
			}
		}
	}

